package edu.umuc.cmsc495_final_project;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;

import javax.swing.JButton;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.ArrayList;


public class newDiner extends JFrame implements WindowFocusListener{
	
	private DataBase db;
	
	private JLabel lblName;
	private JTextField txtFldName;
	private JLabel lblPartyOf;
	private JComboBox cmbPartyOf;
	private JLabel lblDineType;
	private JComboBox cmbDineType;
	private JLabel lblDineTime;
	private JComboBox cmbDineTime;
	public JButton btnMakeReservation;
	private JButton btnCancelReservation;
	
	private String patronName;
	private int patronQuant;
	private DineType patronDineType;
	private RestaurantTime patronDineTime;
	
	private String attendees[] = new String[50];

	/**
	 * Create the frame.
	 */
	public newDiner(DataBase db) {
		this.db = db;
		
		setTitle("New Diner");
		setVisible(true);
		//setBounds(100, 100, 450, 300);
		setSize(450,300);
		new centerScreen(this);
		getContentPane().setLayout(new GridLayout(5, 2));
		
		lblName = new JLabel("Name");
		getContentPane().add(lblName);
		
		txtFldName = new JTextField();
		txtFldName.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				patronName = txtFldName.getText();
			}
		});
		getContentPane().add(txtFldName);
		txtFldName.setColumns(10);
		
		for(int i=0; i<50; i++){
			if(i==49)
				attendees[i] = "50+";
			else
				attendees[i] = String.valueOf(i+1);
		}
		lblPartyOf = new JLabel("Party Of");
		getContentPane().add(lblPartyOf);
		
		cmbPartyOf = new JComboBox(attendees);
		cmbPartyOf.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				patronQuant = cmbPartyOf.getSelectedIndex();
			}
		});
		getContentPane().add(cmbPartyOf);
		
		lblDineType = new JLabel("Dine Type");
		getContentPane().add(lblDineType);
		
		// show reservation time before dine type to avoid null pointer error
		lblDineTime = new JLabel("Dine Time");
		lblDineTime.setVisible(false);
		
		// setup for hours available
		String hours = db.getHoursOfOp();
		int hoursStart = Integer.parseInt(hours.substring(0, hours.indexOf(":")));
		int minStart = Integer.parseInt(hours.substring(hours.indexOf(":")+1, hours.indexOf("-")-1));
		int hoursEnd =Integer.parseInt(hours.substring(hours.indexOf("-")+2, hours.lastIndexOf(":")));
		int minEnd = Integer.parseInt(hours.substring(hours.lastIndexOf(":")+1, hours.length()));
					
		System.out.println("get hours of op " + hours);
		ArrayList<String> restruantTime = new ArrayList<String> ();
		for(int i = hoursStart; i <= hoursEnd; i++){
			for(int j = minStart; j<= minEnd; j+=15){
				if(i > 12){
					if(j==0)
						restruantTime.add((i-12) + ":" + "00 PM");
					else
						restruantTime.add((i-12) + ":" + j + " PM");
				}
				else{
					if(j==0)
						restruantTime.add((i) + ":" + "00 AM");
					else
						restruantTime.add((i) + ":" + j + " AM");
				}
			}
		}
		
		//
		cmbDineTime = new JComboBox(restruantTime.toArray());
		cmbDineTime.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				String newTime = (String) cmbDineTime.getSelectedItem();
				
				System.out.println("this is the time before formating " + newTime);
				int hour = Integer.parseInt( newTime.substring(0, newTime.indexOf(":")));
				int minute = Integer.parseInt( newTime.substring(newTime.indexOf(":")+1, newTime.length()-3));
				if(newTime.indexOf("PM") > 0){
					hour = hour+12;
				}
				System.out.println("the formated time is " + hour + ":" + minute);
				patronDineTime = new RestaurantTime(hour, minute);
			}
		});
		cmbDineTime.setVisible(false);	
		
		//
		cmbDineType = new JComboBox(DineType.values());
		cmbDineType.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				switch((DineType)cmbDineType.getSelectedItem()){
					case RESERVATION:{
						patronDineType = DineType.RESERVATION;
						lblDineTime.setVisible(true);
						cmbDineTime.setVisible(true);
						break;
					}
					case WALKIN:{
						patronDineType = DineType.WALKIN;
						break;
					}
					case BAR:{
						patronDineType = DineType.BAR;
						break;
					}
				}
			}
		});
		getContentPane().add(cmbDineType);
		getContentPane().add(lblDineTime);
		getContentPane().add(cmbDineTime);
			
		//
		btnMakeReservation = new JButton("Make Reservation");
		getContentPane().add(btnMakeReservation);
		
		//
		btnCancelReservation = new JButton("CANCEL Reservation");
		btnCancelReservation.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				patronName = null;
				patronQuant = 0;
				patronDineType = null;
				
				close();
			}
		});
		getContentPane().add(btnCancelReservation);
		
		addWindowFocusListener(this);
	}

	//
	protected patron getDiner(){
		if(patronDineType == DineType.BAR)
			return new patron(patronName, patronDineType, new RestaurantTime().getCurrentTime(), patronQuant , true);
		else
			return new patron(patronName, patronDineType, new RestaurantTime().getCurrentTime(), patronQuant , false);
	}
	
	//
	protected void close() {
		dispose();
	}

	@Override
	public void windowGainedFocus(WindowEvent arg0) {		
	}

	@Override
	public void windowLostFocus(WindowEvent arg0) {
		this.dispose();
	}
}
